package filter

import (
	"file_explorer/model"
	"file_explorer/view/store"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
)

type MinSizeFilter struct {
	content *fyne.Container
	limit   *FileSizeLimit
}

func (filter *MinSizeFilter) GetCanvasObject() fyne.CanvasObject {
	return filter.content
}

func (filter *MinSizeFilter) Filter(fileInfo *model.FileInfo) bool {
	if len(filter.limit.entry.Text) == 0 {
		return true
	}

	size := float64(fileInfo.Size)
	if fileInfo.IsDir() {
		size = 0
	}
	limit := filter.limit.slider.Value * float64(filter.limit.maxSize)
	return size >= limit
}

func (filter *MinSizeFilter) GetValue() string {
	return filter.limit.entry.Text
}

func (filter *MinSizeFilter) LoadValue(val string) {
	filter.limit.entry.Text = val
	filter.limit.entry.Refresh()

	filter.limit.updateSliderByEntry()
}

func (filter *MinSizeFilter) Reset() {
	filter.limit.entry.Text = ""
	filter.limit.slider.Value = 0
	filter.limit.entry.Refresh()
	filter.limit.slider.Refresh()
	filter.limit.refreshMaxSize()
}

func NewMinSizeFilter(tabCtx *store.FileTabContext, onChange func()) *MinSizeFilter {
	filter := &MinSizeFilter{
		limit: newFileSizeLimit(tabCtx, onChange, 0, "min size"),
	}

	filter.content = container.NewVBox(filter.limit.entry, filter.limit.slider)

	return filter
}
